package com.fuchuang.seckillsystem.controller;


import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.fuchuang.seckillsystem.dto.*;
import com.fuchuang.seckillsystem.entity.Goods;
import com.fuchuang.seckillsystem.entity.PreliminarySelect;
import com.fuchuang.seckillsystem.entity.User;
import com.fuchuang.seckillsystem.exception.GlobalException;
import com.fuchuang.seckillsystem.mapper.GoodsMapper;
import com.fuchuang.seckillsystem.mapper.OrderMapper;
import com.fuchuang.seckillsystem.mapper.PreliminarySelectMapper;
import com.fuchuang.seckillsystem.mapper.UserMapper;
import com.fuchuang.seckillsystem.vo.RespBeanEnum;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;

@RestController
@RequestMapping("excel")
public class ExcelController {
    
    @Resource
    private UserMapper userMapper;
    
    @Resource
    private GoodsMapper goodsMapper;
    
    @Resource
    private OrderMapper orderMapper;
    
    @Resource
    private PreliminarySelectMapper preliminarySelectMapper;
    
    //获取用户表格
    @GetMapping("user")
    public void getUserExcel(HttpServletRequest request, HttpServletResponse response, UserQuery userQuery) {
        List<User> users = userMapper.queryAllUser(userQuery);
        getExcel(request, response, users, "用户表", User.class);
    }
    
    //获取商品表格
    @GetMapping("goods")
    public void getGoodsExcel(HttpServletRequest request, HttpServletResponse response, GoodsQuery goodsQuery) {
        List<Goods> goodsList = goodsMapper.selectAllGoods(goodsQuery);
        getExcel(request, response, goodsList, "商品表", Goods.class);
    }
    
    //获取订单表格
    @GetMapping("order")
    public void getOrderExcel(HttpServletRequest request, HttpServletResponse response, OrderQuery orderQuery) {
        List<OrderInfo> orderInfoList = orderMapper.queryAllOrderInfo(orderQuery);
        getExcel(request, response, orderInfoList, "订单表", OrderInfo.class);
    }
    
    //获取初筛记录表格
    @GetMapping("preliminarySelect")
    public void getPreliminarySelect(HttpServletRequest request, HttpServletResponse response, PreliminarySelectQuery preliminarySelectQuery) {
        List<PreliminarySelect> preliminarySelectList = preliminarySelectMapper.selectByQuery(preliminarySelectQuery);
        getExcel(request, response, preliminarySelectList, "初筛记录表", PreliminarySelect.class);
    }
    
    
    private void getExcel(HttpServletRequest request, HttpServletResponse response, List<?> list, String fileName, Class<?> proj) {
        // 告诉浏览器用什么软件可以打开此文件
        response.setHeader("content-Type", "application/vnd.ms-excel");
        //编码
        response.setCharacterEncoding("UTF-8");
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), proj, list);
        try {
            // 下载文件的默认名称
            response.setHeader("response-type", "blob");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".xls");
            workbook.write(response.getOutputStream());
        } catch (Exception e) {
            throw new GlobalException(RespBeanEnum.EXCEL_ERROR);
        } finally {
            if (workbook != null) {
                try {
                    workbook.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        
    }
    
}
